import {Component, OnInit} from '@angular/core';
import {_HttpClient} from '@delon/theme';
import {NzMessageService} from 'ng-zorro-antd/message';
import {NzModalRef} from 'ng-zorro-antd/modal';
import {FormBuilder, FormGroup, Validators} from "@angular/forms";

@Component({
  selector: 'app-sys-permission-edit',
  templateUrl: './edit.component.html',
})
export class SysPermissionEditComponent implements OnInit {
  record: any = {};
  i: any;

  validateForm: FormGroup;
  controlsConfig = {
    ID: [null, null],
    name: ['', [Validators.required]],
    alias: ['', [Validators.required]],
    uri: ['', [Validators.required]],
    method: ['', [Validators.required]],
    group: ['', [Validators.required]],
    description: ['', []],
  };

  constructor(
    private modal: NzModalRef,
    private msgSrv: NzMessageService,
    public http: _HttpClient,
    private fb: FormBuilder,
  ) {
    this.validateForm = this.fb.group(this.controlsConfig);
  }

  ngOnInit(): void {
    if (this.record.ID > 0) {
      console.log(this.record.ID)
      this.http.get(`api/permission/${this.record.ID}`).subscribe(res => {
        this.validateForm.patchValue(res.data);
        this.i = res.data;
      });
    }
  }

  save(value: any): void {
    if (value.ID) {
      this.http.put(`api/permission/` + value.ID, value).subscribe(() => {
        this.msgSrv.success('更新成功！');
        this.modal.close(true);
      });
    } else {
      this.http.post(`api/permission`, value).subscribe(() => {
        this.msgSrv.success('保存成功！');
        this.modal.close(true);
      });
    }
  }

  close(): void {
    this.modal.destroy();
  }
}
